﻿@using Flagrum.Web.Features.AssetExplorer.Data
@using Flagrum.Core.Gfxbin.Gmtl
@inject IStringLocalizer<MaterialPreview> L

<div class="p-6">
    <div class="box p-4 mb-6">
        <h5 class="text-grey-300 mb-2">@BaseMaterial</h5>
        <div class="flex flex-row">
            <div>
                <strong>@L["HighTexturePack"]</strong>
            </div>
            <div class="flex-grow pl-10">
                @(string.IsNullOrWhiteSpace(HighTexturePack) ? "N/A" : HighTexturePack)
            </div>
        </div>
    </div>
    
    @if (TextureInputs.Any())
    {
        <div class="box p-4 mb-6">
            <h5 class="text-grey-300 mb-2">@L["TextureInputs"]</h5>
            <table>
                @foreach (var (name, fileName, path) in TextureInputs)
                {
                    <tr>
                        <td>
                            <strong>@name</strong>
                        </td>
                        <td class="pl-6">
                            <span>@fileName</span>
                            <span class="material-icons pl-1 cursor-pointer text-accent1-200 text-sm pb-0.5"
                                  @onclick="() => ShowPath(path)">
                                open_in_new
                            </span>
                        </td>
                    </tr>
                }
            </table>
        </div>
    }

    @if (HasMaterialInputs)
    {
        <div class="box p-4">
            <h5 class="text-grey-300 mb-2">@L["MaterialInputs"]</h5>
            <table>
                @foreach (var (name, values) in MaterialInputs)
                {
                    <tr>
                        <td>
                            <strong>@name</strong>
                        </td>
                        <td class="pl-6">@string.Join(", ", values)</td>
                    </tr>
                }
            </table>
        </div>
    }
</div>

<AlertModal @ref="AlertModal"/>

@code
{
    private IAssetExplorerNode _previousItem;

    [Parameter]
    public IAssetExplorerNode Item { get; set; }

    private string BaseMaterial { get; set; }
    private IEnumerable<(string Name, float[] Values)> MaterialInputs { get; set; }
    private IEnumerable<(string Name, string FileName, string Path)> TextureInputs { get; set; }
    private AlertModal AlertModal { get; set; }
    private string HighTexturePack { get; set; }
    private bool HasMaterialInputs { get; set; }

    protected override void OnParametersSet()
    {
        if (_previousItem == Item)
        {
            return;
        }

        var reader = new MaterialReader(Item.Data);
        var material = reader.Read();
        BaseMaterial = material.Interfaces[0].Name;

        var inputs = material.InterfaceInputs
            .Where(i => i.InterfaceIndex == 0)
            .OrderBy(i => i.ShaderGenName)
            .Select(i => (i.ShaderGenName, i.Values));

        var textures = material.Textures
            .Where(t => !t.Path.EndsWith(".sb"))
            .OrderBy(t => t.ShaderGenName)
            .Select(t => (t.ShaderGenName, t.Path.Split('/').Last(), t.Path));

        HighTexturePack = material.HighTexturePackAsset;

        HasMaterialInputs = material.InterfaceCount > 0;
        MaterialInputs = inputs;
        TextureInputs = textures;
        StateHasChanged();
    }

    private void ShowPath(string path)
    {
        AlertModal.Open("Texture Path", "Full Data Path", path, null, 700);
    }
}